/**
 * Created by Edwin on 2015/12/16 0016.
 */
var settings = require("./settings");
var url = require('url');
// 材料对象
var Ingredient = require("../Ingredient");
// 菜式
var Cuisine = require("../Cuisine");
var Setmeal = require("../Setmeal");
var Purchase = require("../Purchase");
var IngBuyRecord = require("../IngBuyRecord");
var CashFlow = require("../CashFlow");
var Sale =  require("../Sale");

var adminFunc = {
    siteInfos : function (description) {

        return {
            title : settings.SITETITLE,
            description : description,
            version : settings.SITEVERSION
        }
    },

    getTargetObj : function(currentPage){
        var targetObj;
        if(currentPage.indexOf(settings.INGREDIENT[0]) >=0 ){
            targetObj = Ingredient;
        }else if(currentPage.indexOf(settings.CUISINE[0]) >=0){
            targetObj = Cuisine;
        }else if(currentPage.indexOf(settings.SETMEAL[0]) >=0){
            targetObj = Setmeal;
        }else if(currentPage.indexOf(settings.PURCHASE[0]) >=0){
            targetObj = Purchase;
        }else if(currentPage.indexOf(settings.IngBuyRecord[0]) >=0){
            targetObj = IngBuyRecord;
        }else if(currentPage.indexOf(settings.CashFlow[0]) >=0){
            targetObj = CashFlow;
        }else if(currentPage.indexOf(settings.SALE[0]) >=0){
            targetObj = Sale;
        }

        return targetObj
    },

    setPageInfo : function(req,res,module,currentLink){
        var searchKey = '';
        var startDate="";
        var endDate="";
        if(req.url){
            var params = url.parse(req.url,true);
            searchKey = params.query.searchKey;
            startDate= params.query.startDate;
            endDate= params.query.endDate;
        }
        startDate=startDate==null?new Date().Format("yyyy-MM-dd"):startDate;
        endDate=endDate==null?new Date().Format("yyyy-MM-dd"):endDate;

        searchKey=searchKey==null||searchKey==""?"":searchKey;
        return {
            siteInfo : this.siteInfos(module[1]),
            bigCategory : module[0],
            searchKey : searchKey,
            currentLink : currentLink,
            layout : 'manage/public/adminTpl',
            startDate:startDate,
            endDate:endDate,
            ingredientTypes:settings.INGREDIENTTYPES,
            cuisineTypes:settings.CUISINETYPES,
            cashFlowTypes:settings.CASHFLOWTYPES
        }

    },

    checkAdminPower : function(req,key){
        var power = false;
        var uPower = req.session.adminPower;
        if(uPower){
            var newPowers = eval(uPower);
            for(var i=0;i<newPowers.length;i++) {
                var checkedId = newPowers[i].split(':')[0];
                if(checkedId == key && newPowers[i].split(':')[1]){
                    power = true;
                    break;
                }
            }
        }
        return true||power;
    },

    setDataForInfo : function(infoType,infoContent){
        return {
            siteInfo : this.siteInfos('系统操作提示'),
            bigCategory : 'noticePage',
            infoType : infoType,
            infoContent : infoContent,
            layout: 'manage/public/adminTpl'
        }

    },

    renderToManagePage : function(req,res,url,pageKey){

        if(this.checkAdminPower(req,pageKey[0] + '_view')){
            res.render(url, this.setPageInfo(req,res,pageKey,'/admin/'+url));
        }else{
            res.render("manage/public/notice", this.setDataForInfo('danger','对不起，您无权操作 <strong>'+pageKey[1]+'</strong> 模块！'));
        }
    }
}

module.exports = adminFunc;